class Solution {
public:
    int after_square(int n)
    {
        int outcome = 0;
        while (n)
        {
            int t = n % 10;
            outcome += t * t;
            n /= 10;
        }
        return outcome;
    }

    bool isHappy(int n) {
        int slow = n, fast = after_square(n);
        while (slow != fast)
        {
            slow = after_square(slow);
            fast = after_square(after_square(fast));
        }
        return slow == 1;
    }
};